function WatcherFactory<T>() {
  const callbacks = new Set<(data: T) => void>();
  return {
    emit(data: T) {
      console.log("common emit");
      for (const fn of callbacks) {
        fn(data);
      }
    },
    addListener(fn: (data: T) => void) {
      if (callbacks.has(fn)) {
        throw new Error("listener is already added!");
      }
      callbacks.add(fn);
      return () => {
        callbacks.delete(fn);
      };
    },
    removeListener(fn: (data: T) => void) {
      callbacks.delete(fn);
    },
  };
}

export default WatcherFactory<string>();
